﻿/*
 * Vytvořeno aplikací SharpDevelop.
 * Uživatel: lebeda
 * Datum: 28.5.2012
 * Čas: 21:38
 * 
 * Tento template můžete změnit pomocí Nástroje | Možnosti | Psaní kódu | Upravit standardní hlavičky souborů.
 */
using System;
using System.IO;

using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;

using CommandLine;

//using commons;

namespace locate
{
    class Program
    {
        
        public static void Main(string[] args)
        {
            CmdOptions options = new CmdOptions();
            
            if (args.Length == 0) {
                Console.WriteLine(options.GetUsage());
                return;
            }
            
            if (!CommandLineParser.Default.ParseArguments(args, options)) {
                System.Environment.Exit(0);
            }
            
            Lucene.Net.Store.Directory directory = LocateCommons.GetIndexDirectory();
            IndexSearcher searcher = LocateCommons.getIndexSearcher(directory);
            
            string querystr = options.phrase;
            if (options.normalize) {
                querystr = LocateCommons.LatinToAscii(querystr);
                querystr = querystr.ToLower();
                querystr = "*"+querystr.Trim().Trim('*').Replace(' ','*')+"*";
            }
            
            ScoreDoc[] hits = LocateCommons.getSearchResult(querystr, options.pathToSearch, ref searcher, 10000);
            
            int results = hits.Length;
            for (int i = 0; i < results; i++)
            {
                int docId = hits[i].Doc;
                float score = hits[i].Score;
                
                Document doc = searcher.Doc(docId);
                string resultItem = doc.Get(LocateCommons.COL_KEY);
                if (options.exists) {
                    if (File.Exists(resultItem)) {
                        Console.WriteLine(resultItem);
                    }
                } else {
                    Console.WriteLine(resultItem);
                }
            }
            // Console.WriteLine("Found {0} results", results);
            
        }
    }
}